package com.gexiao.expand.beans.factory;

import cn.hutool.core.lang.Console;
import com.gexiao.expand.beans.Blue;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.*;
import org.springframework.stereotype.Component;

@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    /**
     * BeanDefinitionRegistry bean的定义信息的保存中心，以后BeanFactory就是按照BeanDefinitionRegistry里面保存的每一个bean定义信息来创建bean实例的
     *
     * @param registry
     * @throws BeansException
     */
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        Console.log("MyBeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry...bean的数量：{}", registry.getBeanDefinitionCount());
        // 方式1
//        RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
//        rootBeanDefinition.setBeanClass(Blue.class);
        // 方式2
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Blue.class).getBeanDefinition();
        registry.registerBeanDefinition("hello",beanDefinition);
    }


    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        Console.log("MyBeanDefinitionRegistryPostProcessor#postProcessBeanFactory...bean的数量：{}", beanFactory.getBeanDefinitionCount());

    }
}
